﻿// -----------------------------------------------------------------------
// <copyright file="ImageMapper.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Website.Infrastructure.Mappers
{
	#region Usings

	using MBOP.Core.Entities.Common;
	using MBOP.Website.ViewModels.Common;

	#endregion

	/// <summary>
	/// Provides mapping for image.
	/// </summary>
	public class ImageMapper
	{
		/// <summary>
		/// Maps image view model to image.
		/// </summary>
		/// <param name="imageViewModel">The image view model.</param>
		/// <returns>The image object.</returns>
		public Image MapImage(ImageViewModel imageViewModel)
		{
			Image image = new Image()
			{
				Name = imageViewModel.Name,
				Uri = imageViewModel.Uri
			};

			if (imageViewModel.Content != null && !string.IsNullOrEmpty(imageViewModel.ContentType))
			{
				Data data = new Data()
				{
					Content = imageViewModel.Content,
					ContentLength = imageViewModel.Content.Length,
					ContentType = imageViewModel.ContentType,
					ID = 0
				};

				image.DataID = 0;

				image.Data = data;
			}

			return image;
		}

		/// <summary>
		/// Maps image to image view model.
		/// </summary>
		/// <param name="image">The image.</param>
		/// <returns>The image view model.</returns>
		public ImageViewModel MapImage(Image image)
		{
			ImageViewModel imageViewModel = new ImageViewModel();

			if (image != null)
			{
				imageViewModel = new ImageViewModel()
			   {
				   ID = image.ID,
				   Name = image.Name,
				   Uri = image.Uri,
			   };
			}

			return imageViewModel;
		}
	}
}